/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.front.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.front.entity.IdealLecture;
import com.thinkgem.jeesite.modules.front.service.IdealLectureService;

/**
 * 志愿大讲堂Controller
 * @author 朱俊吉
 * @version 2017-11-28
 */
@Controller
@RequestMapping(value = "${adminPath}/front/idealLecture")
public class IdealLectureController extends BaseController {

	@Autowired
	private IdealLectureService idealLectureService;
	
	@ModelAttribute
	public IdealLecture get(@RequestParam(required=false) String id) {
		IdealLecture entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = idealLectureService.get(id);
		}
		if (entity == null){
			entity = new IdealLecture();
		}
		return entity;
	}
	
	@RequiresPermissions("front:idealLecture:view")
	@RequestMapping(value = {"list", ""})
	public String list(IdealLecture idealLecture, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<IdealLecture> page = idealLectureService.findPage(new Page<IdealLecture>(request, response), idealLecture); 
		model.addAttribute("page", page);
		return "modules/front/idealLectureList";
	}

	@RequiresPermissions("front:idealLecture:view")
	@RequestMapping(value = "form")
	public String form(IdealLecture idealLecture, Model model) {
		model.addAttribute("idealLecture", idealLecture);
		return "modules/front/idealLectureForm";
	}

	@RequiresPermissions("front:idealLecture:edit")
	@RequestMapping(value = "save")
	public String save(IdealLecture idealLecture, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, idealLecture)){
			return form(idealLecture, model);
		}
		idealLectureService.save(idealLecture);
		addMessage(redirectAttributes, "保存志愿讲堂成功");
		return "redirect:"+Global.getAdminPath()+"/front/idealLecture/?repage";
	}
	
	@RequiresPermissions("front:idealLecture:edit")
	@RequestMapping(value = "delete")
	public String delete(IdealLecture idealLecture, RedirectAttributes redirectAttributes) {
		idealLectureService.delete(idealLecture);
		addMessage(redirectAttributes, "删除志愿讲堂成功");
		return "redirect:"+Global.getAdminPath()+"/front/idealLecture/?repage";
	}

}